{extend name="sitehome@style/base"/}
{block name="resources"}
{/block}
{block name="main"}
<div class='nc-function-search'>
	<button class="layui-btn" onclick="location.href ='{:addon_url('DiyView://sitehome/advertisement/edit')}'">添加广告</button>
</div>
<div class="nc-table-box">
    <table id="adv_list" lay-filter="adv" class="layui-table"></table>
</div>

<script type="text/html" id="sort_info">
<input type="number" value="{{d.sort}}" class="layui-input sm" onchange="sortChange({{d.adv_id}}, this)"/>
</script>

<script type="text/html" id="is_show">
	{{# if(d.is_show == 1) {}}
	<span>是</span>
	{{#}else{ }}
	<span>否</span>
	{{#}}}
</script>

<script type="text/html" id="operation">
	<a class="default" lay-event="edit">编辑</a>
	{{# if(d.is_system != 1) {}}
		<a class="default" lay-event="del">删除</a>
	{{#}}}
</script>
{/block}
{block name="script"}
<script>
var table = new Table({
	elem: '#adv_list',
	filter : "adv",
	url : '{:addon_url("DiyView://sitehome/Advertisement/index")}',
	cols: [
		[{
			type: 'checkbox',
            unresize : 'true'
		}, {
			field: 'adv_title',
			width: '20%',
			title: '标题',
            unresize : 'true'
		}, {
            field: 'keywords',
            width: '15%',
            title: '关键词',
            unresize : 'true'
        },{
			field: 'adv_display',
			width: '10%',
			title: '显示方式',
            unresize : 'true',
			templet: function(d){
			    var adv_display = d.adv_display;
			    var name = '';
			    switch (parseInt(adv_display)){
                    case 1 :
                        name = '图片';
                        break;
                    case 2 :
                        name = '轮播';
                        break;
				}
				return name;
			}
		},{
			field: 'is_show',
			width: '10%',
			title: '是否显示',
			templet: '#is_show',
            unresize : 'true'
		},{
			field: 'sort',
			width: '20%',
			title: '排序',
			templet: '#sort_info',
            unresize : 'true'
		},{
			title: '操作',
            align: 'right',
			toolbar: '#operation',
            unresize : 'true'
		}]
	]
});

//监听工具条
table.tool(function(obj) {
	var data = obj.data;
	var event = obj.event;
	if (event === 'edit') {
		window.location.href = nc.url('DiyView://sitehome/advertisement/edit', {"adv_id": data.adv_id});
	} else if (event === 'del') {
		deleteAdv(data.adv_id)
	}
	
});

function deleteAdv(adv_id){
    layer.confirm('确定删除吗?', {
        btn: ['确定','取消']
    }, function(){
        $.ajax({
            type:"post",
            async:false,
			url : '{:addon_url("DiyView://sitehome/advertisement/deleteAdv")}',
            data: { 'adv_id' : adv_id },
            dataType : "JSON",
			success:function(res){
				if(res.code == 0){
					table.reload();
				}
				layer.msg(res.message);
			}
    });
    }, function(){
        layer.close();
    });
}

function sortChange(adv_id, event){
	var sort = $(event).val();
	$.ajax({
		type:"post",
        async:false,
        url : '{:addon_url("DiyView://sitehome/advertisement/sortAdv")}',
		data:{'adv_id':adv_id, 'sort': sort},
		dataType : "JSON",
		success: function (res) {
			layer.msg(res.message);
			table.reload();
		}
	});
}
</script>
{/block}